package com.sdry.service.impl.llm;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.LogManaMapper;
import com.sdry.model.llm.LogMana;
import com.sdry.service.llm.LogManaService;

/** 
* @author lian 
* @version 1.0
* @time 2018年10月16日 下午3:27:39
*/

@Service
public class LogManaServiceImp implements LogManaService {

	@Resource
	private  LogManaMapper logManaMapper;
	
	/**
	 * 查询日志信息总行数
	 * @param logMana 日志实体类
	 * @param response
	 */
	@Override
	public int countLog(LogMana logMana) {
		return logManaMapper.countLog(logMana);
	}

	/**
	 * 分页查询日志信息
	 * @param map 查询条件
	 * @param response
	 */
	@Override
	public List<LogMana> listPageLog(Map<String, Object> map) {
		return logManaMapper.listPageLog(map);
	}

	/**
	 * 保存日志信息
	 * @param logMana 日志实体类
	 * @param response
	 */
	@Override
	public int savaLog(LogMana logMana) {
		return logManaMapper.saveLog(logMana);
	}

	/**
	 * 删除日志信息
	 * @param str 日志ID组成的字符串
	 * @param response
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteLog(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String id : split) {
				isSuccess = logManaMapper.deleteLog(Long.parseLong(id));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**   
	 * 删除一个月以前的日志    
	 * @return    
	 */
	@Override
	public int deleteByMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
		return logManaMapper.deleteByMonth(mon);
	}

}
